<?php
/**
 * Created by PhpStorm.
 * User: Candice俊俊
 * Date: 2016/7/20
 * Time: 13:29
 */

namespace Admin\Model;
use \Think\Model;

class UsersModel extends Model
{
    //添加自动验证规则
    protected $_validate = array(
        array('username', 'require', '请填写账号！', 1),
        array('username', '', '账号已经存在', 0, 'unique', self::MODEL_INSERT),
        array('username',array('administrator','admin'),'用户名不能是administrator或admin',1,'notin'),
        array('pass', 'require', '密码必须！', 1),
        array('pass2','pass','两次密码不一致',0,'confirm'),
        array('realname', 'require', '请填写真实姓名！'),
        array('email', 'require', '请填写邮箱！', 1),
    );

    //添加自动填充规则
    protected $_auto = array(
        //为think_users表的createtime字段生成当前的日期
        array('createtime','date（"Y-m-d"）',self::MODEL_INSERT, 'function'),
        //为think_users表的pass字段进行md5加密
        array('pass','md5', self::MODEL_INSERT, 'function'),
    );

//    function getdate(){
//        return date('Y-m-d');
//    }

    public function register()
    {
        $user = D("Users");
        if ($user->create())
        {
            $user->photo = 'default.jpg';
            if (false !== $user->add())
            {
                $this->success('注册成功！', '');
            }
            else
            {
                $this->error('数据错误');
            }
        }
        else
        {
            $this->error($user->getError());
        }
    }


}